package ali;

/**
 * @author keboom
 * @date 2021/4/27 19:50
 *
 * 一个二叉树中，删除子树中不包含1的子树，返回二叉树
 */
public class Test {
    public TreeNode removeNotHaveOne(TreeNode root) {
        TreeNode parent = root.parent;
        if (root.val == 0 && root.left == null && root.right == null) {
            if (parent.left == root) {
                parent.left = null;
            } else {
                parent.right = null;
            }
            return root;
        } else {
            root.left = removeNotHaveOne(root.left);
            root.right = removeNotHaveOne(root.right);
            return root;
        }
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(0);
        TreeNode c = new TreeNode(1);
        TreeNode d = new TreeNode(1);
        TreeNode e = new TreeNode(0);

        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        Test test = new Test();
        TreeNode treeNode = test.removeNotHaveOne(a);
        System.out.println(a.val);
    }
}
